package com.hjm.java.sql;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

public class ImgTableTool {
    private static final String TAG = "ImgTableTool";

    public static void insert(String id, Bitmap img,  HJMHelper hjmHelper) throws Exception {
        if (id == null || img == null || hjmHelper == null)
            return;

        SQLiteDatabase db = hjmHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("id", id);

        if (isImgIdInDB(id,hjmHelper))
            return;

        // 将 Bitmap 转换为 byte[] 数组
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        img.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        // 进一步处理 byteArray
        // 将 byte[] 数组存入 ContentValues 对象
        values.put("img", byteArray);
        stream.close();

        long resId = db.insert("img", null, values);
        Log.d(TAG, "insert: "+resId);
    }

    /**
     * 获取到img表中不存在的id
     * @param ids 需要检查的ID数组
     * @param hjmHelper
     * @return 不在img表中的ID数组
     */
    public static String[] findImgIdNotInDBArr(String[] ids, HJMHelper hjmHelper) {
        SQLiteDatabase db = hjmHelper.getReadableDatabase();
        ArrayList<String> notFoundIds = new ArrayList<>();

        for (String id : ids) {
            Cursor cursor = db.query("img", new String[]{"id"}, "id=?", new String[]{id}, null, null, null);
            if (!cursor.moveToFirst()) { // 如果游标没有移动到第一行，说明没有找到该id
                notFoundIds.add(id);
            }
            cursor.close(); // 关闭游标以避免内存泄漏
        }

        // 将ArrayList转换为String数组返回
        return notFoundIds.toArray(new String[0]);
    }
    public static boolean isImgIdInDB(String id, HJMHelper hjmHelper) {
        SQLiteDatabase db = hjmHelper.getReadableDatabase();
        ArrayList<String> notFoundIds = new ArrayList<>();

        Cursor cursor = db.query("img", new String[]{"id"}, "id=?", new String[]{id}, null, null, null);
        if (!cursor.moveToFirst()) { // 如果游标没有移动到第一行，说明没有找到该id
            cursor.close(); // 关闭游标以避免内存泄漏
            return false;
        }
        cursor.close(); // 关闭游标以避免内存泄漏

        return true;
    }

    /**
     * 根据id获取图片
     * @param id
     * @param hjmHelper
     * @return
     */
    public static Bitmap findImgById(String id,  HJMHelper hjmHelper) {
        SQLiteDatabase db = hjmHelper.getReadableDatabase();

        Cursor cursor = db.query("img", new String[]{"img"}, "id=?", new String[]{id}, null, null, null);
        if (!cursor.moveToFirst()) { // 如果游标没有移动到第一行，说明没有找到该id
            return null;
        }
        byte[] imgBytes = cursor.getBlob(0);
        cursor.close();

        // 将 byte[] 数组转换为 Bitmap 对象
        Bitmap img = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length);

        return img;
    }
}
